package com.robotserver.orders.web;

import com.alibaba.fastjson.JSONObject;
import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.constant.CacheKey;
import com.robotserver.orders.constant.ResponseCode;
import com.robotserver.orders.constant.ResponseMsg;
import com.robotserver.orders.entity.TbOrders;
import com.robotserver.orders.exception.BusinessException;
import com.robotserver.orders.param.TbOrderRefundSyncParam;
import com.robotserver.orders.param.TbOrderSyncParam;
import com.robotserver.orders.param.TbOrderSyncParamV3;
import com.robotserver.orders.param.biz.TbOrderPullParam;
import com.robotserver.orders.service.TbOrderManualService;
import com.robotserver.orders.service.TbOrderRefundService;
import com.robotserver.orders.service.TbOrderRefundSyncService;
import com.robotserver.orders.service.TbOrderService;
import com.robotserver.orders.vo.TbkOrderTimeVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: jianghan
 * @Date: 2019/11/14 09:42
 * @Description:
 */
@RestController
public class TbOrderRefundController {

    private static Logger logger = LoggerFactory.getLogger(TbOrderRefundController.class);

    @Autowired
    private TbOrderRefundService tbOrderRefundService;

    /**
     * 本地下载
     * @param param
     * @return
     */
    @PostMapping("tbOrderRefund/pull")
    public Object pullOrderRefund(TbOrderPullParam param){
        return tbOrderRefundService.getOrderList(param);
    }

    /**
     * 远程下载单个Token
     * @param tbOrderRefundSyncParam
     * @return
     */
    @PostMapping("tbOrderRefund/remotePull")
    public Object remotePull(TbOrderRefundSyncParam tbOrderRefundSyncParam){

        logger.info("--2.0手动远程下载淘宝维权订单--tbOrderRefundSyncParam={}", JSONObject.toJSONString(tbOrderRefundSyncParam));

        if (tbOrderRefundSyncParam.getUserId()==null || StringUtils.isBlank(tbOrderRefundSyncParam.getUserName())
                || StringUtils.isBlank(tbOrderRefundSyncParam.getAccessToken()) || tbOrderRefundSyncParam.getBegin_time()==null  || tbOrderRefundSyncParam.getEnd_time()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //判断开始时间是否小于90天前
        Integer now = DateUtil.getNowTime();
        if(tbOrderRefundSyncParam.getBegin_time() < (Long.valueOf(now)-7776000)){
            logger.error("------远程单个token错误，开始时间不能小于90天前。-------"+tbOrderRefundSyncParam.getAccessToken()+"::"+tbOrderRefundSyncParam.getUserName()+"::"+tbOrderRefundSyncParam.getBegin_time()+"::"+tbOrderRefundSyncParam.getEnd_time());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.STARTTIME_ERROR);
        }

        //如果查询时间类型未填写，默认未5-维权订单更新时间查询
        if(tbOrderRefundSyncParam.getType() == null){
            tbOrderRefundSyncParam.setType(5);
        }

        tbOrderRefundService.remotePull(tbOrderRefundSyncParam);
        return true;
    }


    /**
     * 远程下载单个联盟的维权订单
     * @param tbOrderRefundSyncParam
     * @return
     */
    @PostMapping("tbOrderRefund/remotePullV3")
    public Object remotePullV3(TbOrderRefundSyncParam tbOrderRefundSyncParam){

        logger.info("--3.0手动远程下载淘宝维权订单--tbOrderRefundSyncParam={}", JSONObject.toJSONString(tbOrderRefundSyncParam));

        if (tbOrderRefundSyncParam.getUserId() == null
                || StringUtils.isBlank(tbOrderRefundSyncParam.getUserName())
                || StringUtils.isBlank(tbOrderRefundSyncParam.getAccessToken())
                || tbOrderRefundSyncParam.getBegin_time() == null
                || tbOrderRefundSyncParam.getEnd_time() == null
        ){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //判断开始时间是否小于90天前
        Integer now = DateUtil.getNowTime();
        if(tbOrderRefundSyncParam.getBegin_time() < (Long.valueOf(now)-7776000)){
            logger.error("------远程单个token错误，开始时间不能小于90天前。-------"+tbOrderRefundSyncParam.getAccessToken()+"::"+tbOrderRefundSyncParam.getUserName()+"::"+tbOrderRefundSyncParam.getBegin_time()+"::"+tbOrderRefundSyncParam.getEnd_time());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.STARTTIME_ERROR);
        }

        //如果查询时间类型未填写，默认未5-维权订单更新时间查询
        if(tbOrderRefundSyncParam.getType() == null){
            tbOrderRefundSyncParam.setType(5);
        }

        tbOrderRefundService.remotePull(tbOrderRefundSyncParam);
        return true;
    }

    /**
     * 远程补拉淘宝维权全部联盟订单-3.0V
     * @param tbOrderRefundSyncParam
     * @return
     */
    @PostMapping("tbOrderRefund/pullOrderByAllToken")
    public Object pullOrderByAllToken(TbOrderRefundSyncParam tbOrderRefundSyncParam){

        logger.info("--god平台手动远程下载淘宝维权订单--tbOrderRefundSyncParam={}", JSONObject.toJSONString(tbOrderRefundSyncParam));

        if (tbOrderRefundSyncParam.getBegin_time() == null
                || tbOrderRefundSyncParam.getEnd_time() == null
        ){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //判断开始时间是否小于90天前
        Integer now = DateUtil.getNowTime();
        if(tbOrderRefundSyncParam.getBegin_time() < (Long.valueOf(now)-7776000)){
            logger.error("------god平台手动远程下载淘宝维权订单，开始时间不能小于90天前。-------"+tbOrderRefundSyncParam.getBegin_time()+"::"+tbOrderRefundSyncParam.getEnd_time());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.STARTTIME_ERROR);
        }

        //如果查询时间类型未填写，默认未4-订单创建时间
        if(tbOrderRefundSyncParam.getType() == null){
            tbOrderRefundSyncParam.setType(4);
        }

        //淘宝维权渠道订单
        TbOrderRefundSyncParam tbOrdersRefundSyncParam_channel = new TbOrderRefundSyncParam();
        tbOrdersRefundSyncParam_channel.setBegin_time(tbOrderRefundSyncParam.getBegin_time());
        tbOrdersRefundSyncParam_channel.setEnd_time(tbOrderRefundSyncParam.getEnd_time());
        tbOrdersRefundSyncParam_channel.setType(tbOrderRefundSyncParam.getType());
        tbOrdersRefundSyncParam_channel.setBizType(1L);  //1代表渠道订单
        tbOrdersRefundSyncParam_channel.setRedisQueueKey(CacheKey.tbOrderRefundQueue);
        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
        tbOrderRefundService.syncOrder_producer(tbOrdersRefundSyncParam_channel);

        return true;
    }

}
